package cate.game.framework.advanced;

import com.google.common.collect.Lists;

import java.util.LinkedList;
import java.util.Objects;

public class FIFOAdvancedTimerEventSelector<TIMER_EVENT extends AdvancedTimerEvent>
        implements AdvancedTimerEventSelector<TIMER_EVENT> {

    private final LinkedList<TIMER_EVENT> taskQueue = Lists.newLinkedList();

    public void offer(TIMER_EVENT timerEvent) {
        this.taskQueue.offer(timerEvent);
    }

    @Override
    public TIMER_EVENT select(Long aLong) {
        TIMER_EVENT timerEvent = this.taskQueue.peek();
        if (Objects.nonNull(timerEvent) && timerEvent.isMatch(System.currentTimeMillis())) {
            return this.taskQueue.poll();
        }
        return null;
    }
}
